﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace TwitterClient2.Components
{

    public class ImageButton : Control
    {
        //Private members    
        private Image image;
        //flag to indicate the pressed state
        private Image normalImage;
        private Image mousePressedImage;
        private bool buttonPushed;
        private Bitmap m_bmpOffscreen;
        public ImageButton()
        {
            buttonPushed = false;


        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Graphics gxOff;   //Offscreen graphics 
            Rectangle imgRect; //image rectangle 
            Brush backBrush;   //brush for filling a backcolor 
            if (m_bmpOffscreen == null) //Bitmap for doublebuffering 
            {
                m_bmpOffscreen = new Bitmap(ClientSize.Width, ClientSize.Height);
            }
            gxOff = Graphics.FromImage(m_bmpOffscreen);
            gxOff.Clear(this.BackColor);

            backBrush = new SolidBrush(Parent.BackColor);

            gxOff.FillRectangle(backBrush, this.ClientRectangle);
            if (this.buttonPushed)
                image = mousePressedImage;
            else
                image = normalImage;
            if (image != null)
            {

                if (!buttonPushed)
                {
                    imgRect = new Rectangle(0, 0, image.Width,
                  image.Height);
                }
                else //The button was pressed 
                {
                    //Shift the image by one pixel 
                    imgRect = new Rectangle(1, 1, image.Width,
                  image.Height);
                }
                //Set transparent key 
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorKey(BackgroundImageColor(image),
               BackgroundImageColor(image));
                //Draw image 
                gxOff.DrawImage(image, imgRect, 0, 0, image.Width, image.Height,
                GraphicsUnit.Pixel, imageAttr);
            }
            //Draw from the memory bitmap 
            e.Graphics.DrawImage(m_bmpOffscreen, 0, 0);
            base.OnPaint(e);

        }
        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            buttonPushed = true;
            this.Invalidate();
        }
        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            buttonPushed = false;
            this.Invalidate();
        }




        private Color BackgroundImageColor(Image image)
        {
            Bitmap bmp = new Bitmap(image);
            return bmp.GetPixel(0, 0);
        }
        public Image NormalImage
        {
            get
            {
                return normalImage;
            }
            set
            {
                normalImage = value;
            }
        }

        public Image MousePressedImage
        {

            get
            {
                return mousePressedImage;
            }
            set
            {
                mousePressedImage = value;
            }
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // ImageButton
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Size = new System.Drawing.Size(60, 60);
            this.ResumeLayout(false);

        }


    }

}
